<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Pisces","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="Servlet，request，response">
<meta property="og:type" content="article">
<meta property="og:title" content="Java日拱一卒-day19">
<meta property="og:url" content="http://yoursite.com/2020/05/18/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day19/index.html">
<meta property="og:site_name" content="很惭愧，就做了一点微小的工作">
<meta property="og:description" content="Servlet，request，response">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://yoursite.com/2020/05/18/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day19/Servlet%E7%A8%8B%E5%BA%8F%E5%AE%9E%E7%8E%B0.png">
<meta property="og:image" content="http://yoursite.com/2020/05/18/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day19/%E9%87%8D%E5%AE%9A%E5%90%91.png">
<meta property="article:published_time" content="2020-05-18T03:51:08.000Z">
<meta property="article:modified_time" content="2020-05-19T09:14:36.096Z">
<meta property="article:author" content="文予">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://yoursite.com/2020/05/18/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day19/Servlet%E7%A8%8B%E5%BA%8F%E5%AE%9E%E7%8E%B0.png">

<link rel="canonical" href="http://yoursite.com/2020/05/18/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day19/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Java日拱一卒-day19 | 很惭愧，就做了一点微小的工作</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">很惭愧，就做了一点微小的工作</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">念念不忘，必有回响</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/05/18/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day19/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/wenyu.png">
      <meta itemprop="name" content="文予">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="很惭愧，就做了一点微小的工作">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Java日拱一卒-day19
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-05-18 11:51:08" itemprop="dateCreated datePublished" datetime="2020-05-18T11:51:08+08:00">2020-05-18</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-05-19 17:14:36" itemprop="dateModified" datetime="2020-05-19T17:14:36+08:00">2020-05-19</time>
              </span>

          
            <div class="post-description">Servlet，request，response</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p><code>Servlet</code>是使用java语言编写的服务器端程序，可以像jsp一样生成动态的Web页面。与之前的java程序不同的是，<code>Servlet</code>程序无法独立运行，需要将<code>Servlet</code>程序放在容器中（例如Tomcat服务器），由服务器调用。</p>
<p><code>Servlet</code>程序按照如下步骤进行处理：</p>
<ul>
<li>(1) 客户端（一般为Web浏览器）通过HTTP提出请求</li>
<li>(2) Web服务器接收该请求并发给<code>Servlet</code>。如果这个<code>Servlet</code>尚未加载，web服务器将会把他加载到Java虚拟机并执行它</li>
<li>(3) <code>Servlet</code>程序将接受该Http请求并执行某种处理</li>
<li>(4) <code>Servlet</code>将处理后的结果向Web服务器返回应答</li>
<li>(5) Web服务器将从<code>Servlet</code>收到的应答发回给客户端</li>
</ul>
<h2 id="开发Servlet程序"><a href="#开发Servlet程序" class="headerlink" title="开发Servlet程序"></a>开发<code>Servlet</code>程序</h2><p>第一步：写一个类，实现一个<code>Servlet</code>接口或者继承<code>Servlet</code>的子类（GenericServlet/HttpServlet），并实现其中的方法</p>
<p><img src="/2020/05/18/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day19/Servlet%E7%A8%8B%E5%BA%8F%E5%AE%9E%E7%8E%B0.png" alt="Servlet程序实现"></p>
<p>第二步：在Web应用的web.xml文件中配置<code>Servlet</code>程序对外访问的路径</p>
<h3 id="使用Eclipse创建Web项目"><a href="#使用Eclipse创建Web项目" class="headerlink" title="使用Eclipse创建Web项目"></a>使用Eclipse创建Web项目</h3><ul>
<li><ol>
<li>创建一个Web工程</li>
</ol>
</li>
<li><ol start="2">
<li>在该工程下创建一个<code>Servlet</code></li>
</ol>
</li>
</ul>
<p>通过Eclipse创建<code>Servlet</code>，默认继承HttpServlet，当然也是间接的实现了<code>Servlet</code>接口</p>
<p>继承HttpServlet类，默认会覆写<code>doGet</code>和<code>doPost</code>方法，这两个方法的作用为：</p>
<ul>
<li><code>doGet</code>方法：当浏览器发送的请求方式为GET提交时，调用<code>doGet</code>方法</li>
<li><code>doPost</code>方法：浏览器发送的请求方式为POST提交时，调用<code>doPost</code>方法</li>
</ul>
<h3 id="Servlet在web-xml中的配置"><a href="#Servlet在web-xml中的配置" class="headerlink" title="Servlet在web.xml中的配置"></a>Servlet在web.xml中的配置</h3><p>每创建一个Servlet，都要在web.xml中进行Servlet的相关信息的配置（一般由IDE自动完成）</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>HelloServlet<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>com.tedu.HelloServlet<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>HelloServlet<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/HelloServlet<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="request和response介绍"><a href="#request和response介绍" class="headerlink" title="request和response介绍"></a>request和response介绍</h2><p>request是代表http请求信息的对象，response的代表http响应信息的对象。</p>
<p>当浏览器发出请求访问服务器的某一个Servlet时，服务器会调用Servlet中的service方法来处理请求。在调用service方法之前会创建出request和response对象。</p>
<p>其中request对象中封装了浏览器发送给服务器的请求信息（请求行、请求头、请求实体等），response对象中将会封装服务器中要发送给浏览器的响应信息（状态行、响应头、响应实体），在service方法执行完之后，服务器再讲response中的数据取出，按照http的协议格式发送给浏览器。</p>
<p>每次浏览器访问服务器，服务器在调用service方法处理请求之前都会创建request和response对象（也即服务器每次处理请求都会创建request和response对象）。在请求处理完，响应结束后，服务器会销毁request和response对象。</p>
<h3 id="request对象"><a href="#request对象" class="headerlink" title="request对象"></a>request对象</h3><h4 id="获取请求参数"><a href="#获取请求参数" class="headerlink" title="获取请求参数"></a>获取请求参数</h4><p>请求参数是指浏览器发送给服务器的数据（不论请求方式），例如通过表单想服务器提交的用户名、密码等，或者在超链接后面通过问号提交的数据等</p>
<p>获取请求参数的方法有两种：</p>
<ul>
<li><ol>
<li><code>request.getParameter(String paramName)</code>：根据参数名称获取参数值，返回一个字符串。如果有多个值，返回第一个值。如果获取的是一个不存在的参数，则返回<code>null</code></li>
</ol>
</li>
<li><ol start="2">
<li><code>request.getParameterValues(String paramName)</code>：根据请求参数的名字获取该名字对应的所有参数值组成的数组，返回值是一个字符串数组，其中包含了这个参数名对应的所有参数值。如果获取的是一个不存在的参数，则返回<code>null</code></li>
</ol>
</li>
</ul>
<p>代码示例如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//1.获取请求参数中的用户名(user)</span></span><br><span class="line">String user = request.getParameter(<span class="string">"user"</span>);</span><br><span class="line">System.out.println( <span class="string">"user="</span>+user );</span><br><span class="line"></span><br><span class="line"><span class="comment">//2.获取请求参数中的爱好(like)</span></span><br><span class="line">String[] like = request.getParameterValues( <span class="string">"like"</span> );</span><br><span class="line">System.out.println( <span class="string">"like="</span>+Arrays.toString( like ) );</span><br></pre></td></tr></table></figure>

<h4 id="实现请求转发"><a href="#实现请求转发" class="headerlink" title="实现请求转发"></a>实现请求转发</h4><p>请求转发是服务器内部资源的一种跳转方式，既当浏览器发送请求访问服务器中的某一个资源A时，该资源将请求转交给另外一个资源B进行处理，并且由B做出响应的过程。</p>
<p>请求转发的特点：</p>
<ul>
<li>一次请求，一次响应</li>
<li>请求转发前后，地址栏地址不会发生变化</li>
<li>请求转发前后的request对象是同一个</li>
<li>请求转发前后两个资源必须属于同一个Web应用</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">request.getRequestDispatcher(url地址/转发到资源的地址).forward(req, res);</span><br></pre></td></tr></table></figure>

<p>例如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//从当前Servlet转发到 index.jsp(http://localhost/day10/index.jsp)</span></span><br><span class="line">request.getRequestDispatcher(<span class="string">"index.jsp"</span>).forward(request, response);</span><br></pre></td></tr></table></figure>

<h4 id="案例-模拟查询所有门店功能"><a href="#案例-模拟查询所有门店功能" class="headerlink" title="案例:模拟查询所有门店功能"></a>案例:模拟查询所有门店功能</h4><ul>
<li><ol>
<li>创建一个Servlet程序，用于处理查询所有门店信息请求</li>
</ol>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DoorListServlet</span> <span class="keyword">extends</span> <span class="title">HttpServlet</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = <span class="number">1L</span>;</span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">doGet</span><span class="params">(HttpServletRequest request, HttpServletResponse</span></span></span><br><span class="line"><span class="function"><span class="params">response)</span></span></span><br><span class="line"><span class="function"><span class="keyword">throws</span> ServletException, IOException </span>&#123;</span><br><span class="line">        response.setContentType(<span class="string">"text/html;charset=utf-8"</span>);</span><br><span class="line">        PrintWriter out = response.getWriter();</span><br><span class="line">        <span class="comment">//1.模拟查询数据库, 查询所有门店集合</span></span><br><span class="line">        List&lt;String&gt; doorList = <span class="keyword">new</span> ArrayList();</span><br><span class="line">        doorList.add(<span class="string">"01, 永和北三环西路店, 010-67676767"</span>);</span><br><span class="line">        doorList.add(<span class="string">"02, 永和西直门店, 010-68976347"</span>);</span><br><span class="line">        doorList.add(<span class="string">"03, 永和东直门店, 010-78397647"</span>);</span><br><span class="line">        doorList.add(<span class="string">"04, 永和北京西店, 010-78764397"</span>);</span><br><span class="line">        doorList.add(<span class="string">"05, 永和天安门店, 010-78769743"</span>);</span><br><span class="line">        <span class="comment">//2.将数据存入request域中</span></span><br><span class="line">        request.setAttribute( <span class="string">"list"</span>, doorList );</span><br><span class="line">        <span class="comment">//3.将请求转发到 door_list.jsp 中, 取出所有门店显示在页面中</span></span><br><span class="line">        request.getRequestDispatcher(<span class="string">"door_list.jsp"</span>)</span><br><span class="line">        .forward(request, response);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><ol start="2">
<li>创建一个JSP，用于显示所有门店信息</li>
</ol>
</li>
</ul>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;body&gt;</span><br><span class="line">&lt;h3&gt;显示所有门店信息&lt;/h3&gt;</span><br><span class="line">    &lt;%</span><br><span class="line">        <span class="comment">//获取request域中的门店信息集合</span></span><br><span class="line">        List&lt;String&gt; list =(List&lt;String&gt;)request.getAttribute(<span class="string">"list"</span>);</span><br><span class="line">        <span class="comment">//遍历门店集合, 将门店信息输出在网页上</span></span><br><span class="line">        <span class="keyword">for</span>( String door : list )&#123;</span><br><span class="line">            out.write( door +<span class="string">"&lt;br/&gt;"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    %&gt;</span><br><span class="line">&lt;/body&gt;</span><br></pre></td></tr></table></figure>

<h3 id="response对象"><a href="#response对象" class="headerlink" title="response对象"></a>response对象</h3><p>response的代表http响应信息的对象</p>
<h4 id="向客户端发送数据"><a href="#向客户端发送数据" class="headerlink" title="向客户端发送数据"></a>向客户端发送数据</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">PrintWriter out = response.getWriter();</span><br></pre></td></tr></table></figure>

<p>由于服务器在通过response获取的流发送数据时，默认使用iso8859-1编码，而这个编码中没有中文字符，所以在通过response获取的流发送中文数据时，会出现乱码问题。</p>
<p>解决方法是：在响应数据之前，通知服务器使用utf-8发送数据。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*  通知服务器在响应数据时,使用utf-8编码</span></span><br><span class="line"><span class="comment"> * 也能通知浏览器使用utf-8接收服务器发送的数据 */</span></span><br><span class="line">response.setContentType( <span class="string">"text/html;charset=utf-8"</span> );</span><br><span class="line">PrintWriter out = response.getWriter();</span><br><span class="line">out.write( <span class="string">"你好"</span> );</span><br></pre></td></tr></table></figure>

<p><img src="/2020/05/18/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day19/%E9%87%8D%E5%AE%9A%E5%90%91.png" alt="重定向"></p>
<p>重定向的特点：</p>
<ul>
<li>两次请求，两次响应</li>
<li>重定向前后，地址栏的地址会变化</li>
<li>重定向前后的request对象不是同一个（因为实际上创建了两个request对象）</li>
<li>重定向前后的两个资源可以是来自不同的web应用，甚至是不同的服务器</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">response.sendRedirect(所重定向到资源的URL地址);</span><br></pre></td></tr></table></figure>

<p>例如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//测试1: 从当前Servlet（day10/TestRedirect）重定向到day10/index.jsp</span></span><br><span class="line"><span class="comment">// http://localhost/day10/TestRedirect</span></span><br><span class="line"><span class="comment">// http://localhost/day10/index.jsp</span></span><br><span class="line">response.sendRedirect( <span class="string">"http://localhost/day10/index.jsp"</span> );</span><br><span class="line">response.sendRedirect( <span class="string">"/day10/index.jsp"</span> );</span><br><span class="line">response.sendRedirect( <span class="string">"/index.jsp"</span> ); <span class="comment">//错误路径</span></span><br><span class="line">response.sendRedirect( <span class="string">"index.jsp"</span> ); <span class="comment">//正确路径</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//测试2: 从当前Servlet重定向到day09/index.jsp</span></span><br><span class="line">response.sendRedirect( <span class="string">"http://localhost/day09/index.jsp"</span> );</span><br><span class="line"></span><br><span class="line"><span class="comment">//测试3: 从当前Servlet重定向到百度首页</span></span><br><span class="line">response.sendRedirect( <span class="string">"http://www.baidu.com"</span> );</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/05/15/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day18/" rel="prev" title="Java日拱一卒-day18">
      <i class="fa fa-chevron-left"></i> Java日拱一卒-day18
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/05/20/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day20/" rel="next" title="Java日拱一卒-day20">
      Java日拱一卒-day20 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#开发Servlet程序"><span class="nav-number">1.</span> <span class="nav-text">开发Servlet程序</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#使用Eclipse创建Web项目"><span class="nav-number">1.1.</span> <span class="nav-text">使用Eclipse创建Web项目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Servlet在web-xml中的配置"><span class="nav-number">1.2.</span> <span class="nav-text">Servlet在web.xml中的配置</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#request和response介绍"><span class="nav-number">2.</span> <span class="nav-text">request和response介绍</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#request对象"><span class="nav-number">2.1.</span> <span class="nav-text">request对象</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#获取请求参数"><span class="nav-number">2.1.1.</span> <span class="nav-text">获取请求参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实现请求转发"><span class="nav-number">2.1.2.</span> <span class="nav-text">实现请求转发</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#案例-模拟查询所有门店功能"><span class="nav-number">2.1.3.</span> <span class="nav-text">案例:模拟查询所有门店功能</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#response对象"><span class="nav-number">2.2.</span> <span class="nav-text">response对象</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#向客户端发送数据"><span class="nav-number">2.2.1.</span> <span class="nav-text">向客户端发送数据</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="文予"
      src="/uploads/wenyu.png">
  <p class="site-author-name" itemprop="name">文予</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">29</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">文予</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>


        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

</body>
</html>
